<style lang="scss" scoped>

</style>
<template>
  <page-box>
    <div class="ui-search-box" slot="search-form">
      <el-form class="ui-search-form" label-width="120px" :model="params" ref="form">
        <el-form-item label="用户名" prop="username">
          <el-input size="mini" class="ui-item-reset" v-model="params.username" clearable></el-input>
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input size="mini" class="ui-item-reset" v-model="params.userId" clearable></el-input>
        </el-form-item>
        <el-form-item label="每页数据">
          <el-select size="mini" class="ui-item-reset"  v-model="params.pageSize">
            <el-option label="5" :value="5"></el-option>
            <el-option label="10" :value="10"></el-option>
            <el-option label="20" :value="20"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="ui-search-btus">
        <el-button type="primary" size="mini" @click="getData">搜索</el-button>
        <el-button  size="mini" @click="resetForm('form')">重置</el-button>
      </div>
    </div>
    <div class="ui-list ui-box" slot="list-box">
      <div class="box-header ui-list-header">
        <div>用户列表</div>
        <div>
          <el-button type="primary" size="mini" @click="exportExcel">导出</el-button>
          <el-button type="primary" size="mini" @click="open">新增</el-button>
        </div>
      </div>
      <el-table :data="dataList" @selection-change="handleSelectionChange" :cell-style="cellStyle"
                :header-cell-style="rowClass" v-loading="tabLoading">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="userId" label="用户id"></el-table-column>
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column prop="roleId" label="角色">
          <template v-if="scope.row.roleId!=null" slot-scope="scope">
            <span v-if="scope.row.roleId===1">超级管理员</span>
            <span v-if="scope.row.roleId===2">管理员</span>
            <span v-if="scope.row.roleId===3">学生</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-button type="success" icon="el-icon-check" circle size="mini" v-if="scope.row.status==1"></el-button>
            <el-button type="danger" icon="el-icon-close" circle v-else size="mini"></el-button>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
        <el-table-column prop="updateTime" label="更新时间" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="checkDetail(scope.row)">详情</el-button>
            <el-button size="mini" type="danger" @click="deleteItem(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="ui-pagination"
        @current-change="changPageNum"
        layout="total, prev, pager, next"
        :current-page="this.params.pageNum"
        :page-size="this.params.pageSize"
        :total="total">
      </el-pagination>
      <add-item v-bind:userItem.sync='userItem' @submit="addItem" ref="addPerm"></add-item>
    </div>
  </page-box>
</template>
<script>
  import pageBox from '@/components/pageBox'
  import addItem from '@/components/user/addItem.vue'

  export default {
    name: 'userList',
    components: {
      pageBox, addItem
    },
    data() {
      return {
        dataList: [],
        exportList: [],
        test: '1',
        userItem: {//必须写，不然addItem清空后无法输入
          username:'',
          password:''
        },
        params: {
          pageNum: 1,
          pageSize: 10,
          username: '',
          userId: '',
        },
        total:0,
        tabLoading: false,
      }
    },
    created() {
      this.getData()
    },
    methods: {
      changPageNum(val){
        this.params.pageNum=val
        this.getData()
      },
      getData() {
        post('/pms_api/user/list', this.params).then(res => {
          this.dataList = res.data
          this.total = res.totalPages
          this.tabLoading = false
        }).catch(err => {
          this.tabLoading = false
          console.log(err)
        })
      },
      addItem() {
        this.tabLoading = true
        post('/pms_api/user/add', this.userItem).then(res => {
          this.$refs.addPerm.addDialogVisible = false;
          console.log('res', res);
          this.getData();
        }).catch(err => {
          console.log(err)
          this.tabLoading = false
        })
        this.$refs.addPerm.addDialogVisible = false;
      },
      open() {
        // this.addDialogVisible=!this.addDialogVisible
        this.$refs.addPerm.openWindow('新增用户')
      },
      checkDetail(item)  {
        this.$router.push({
          path: '/userDetail',
          query: {
            title: item.name,
            id: item.userId
          }
        })
      },
      deleteItem(item) {
        post('/pms_api/user/delete', {'userId': item.userId}).then(res => {
          this.getData()
        }).catch(err => {
          console.log(err)
          this.tabLoading = false
        })
      },
      handleSelectionChange(val) {
        this.exportList = val
      },
      exportExcel() {
        let sexName = {2: '女', 1: '男'};
        if (this.exportList.length > 0) {
          this.exportList.forEach(item => {
            item.sexName = sexName[item.sex]
          });
          const tHeader = ['用户id', '用户名', '状态', '创建时间', '更新时间', '上次登录时间'];
          const filterVal = ['userId', 'username', 'status', 'createTime', 'updateTime', 'lastLoginTime'];
          const list = this.exportList;
          this.$getGlobal.exportFun('用户列表', tHeader, filterVal, list);
        } else {
          this.$message.error('请先选择需要导出的列表项');
        }
      },
      //表格居中
      cellStyle({row, colum, rowIndex, columIndex}) {
        return "text-align: center";
      },
      rowClass({row, rowIndex}) {
        return "text-align: center";
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.getData()
      },

    }
  }
</script>


